package persistency;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import model.Task;

public class TaskRepository implements Repository<Task> {

	@Override
	public void insert(Task entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			em.persist(entity);
			em.flush();
			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new PersistencyException("Insert failed!", ex);
		} finally {
			em.close();
		}

	}

	@Override
	public void update(Task entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			em.merge(entity);
			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new PersistencyException("Update failed!", ex);
		} finally {
			em.close();
		}
	}

	@Override
	public void delete(Task entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			Task dTask = getById(entity.getTaskId());
			em.remove(dTask);
			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new PersistencyException("Delete failed!", ex);
		} finally {
			em.close();
		}

	}

	@Override
	public List<Task> getAll() {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			List<Task> allTasks = em.createQuery("Select t from Task t").getResultList();
			
			
			
			
			tx.commit();
			return allTasks;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Task> getAllActive() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			List<Task> allTasks = em.createQuery(
					"Select t from Task t where active = true")
					.getResultList();
			tx.commit();
			return allTasks;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
		return null;
	}

	@Override
	public Task getById(Object id) {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			Task gTask = em.find(Task.class, id);
			tx.commit();
			return gTask;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
		return null;
	}

	@Override
	public int getCount() {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			int pCount = Integer.parseInt(em
					.createQuery("Select count(t.taskId) from Task t")
					.getSingleResult().toString());
			tx.commit();
			return pCount;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			em.close();
		}
		return 0;
	}
}
